package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        System.out.println(checkGroupId);
        if (checkitemIds.length>0){
            checkGroupDao.insertGroupAndCheckitemId(checkGroupId,checkitemIds);
        }
    }

    @Override
    public void handleEdit(CheckGroup checkGroup,Integer[] checkitemIds) {
        //修改检查组基本信息
        checkGroupDao.handleEdit(checkGroup);
        //清理中间关系表t_checkgroup_checkitem表
        checkGroupDao.deleteByCheckGroupId(checkGroup.getId());
        if (checkitemIds.length>0){
            //重新建立关系
            checkGroupDao.insertGroupAndCheckitemId(checkGroup.getId(),checkitemIds);
        }
    }
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> checkGroups = checkGroupDao.findByQueryString(queryString);
        for (CheckGroup checkGroup : checkGroups) {
            int count = checkGroupDao.findCountByCheckGroupId(checkGroup.getId());
            checkGroup.setCheckitemIds(count);
        }
        List<CheckGroup> rows = checkGroups.getResult();
        long total = checkGroups.getTotal();
        return new PageResult(total,rows);
    }

    @Override
    public void deleteByid(Integer id) {
        checkGroupDao.deleteByCheckGroupId(id);
        checkGroupDao.deleteByid(id);
    }

    @Override
    public CheckGroup handleUpdate(Integer id) {
        return checkGroupDao.handleUpdate(id);
    }

    @Override
    public Integer[] findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    //建立检查组和检查项多对多关系
//    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
//        if (checkitemIds!=null&&checkitemIds.length>0){
//            for (Integer checkitemId : checkitemIds) {
//                checkGroupDao.associationItemAndGroup(checkGroupId,checkitemId);
//            }
//        }
//    }
}
